/**
 * Copyright 2013-2015
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */	  

package com.cos.controller.common;

import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.NumberUtils;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

/**
 * token的一些操作
 * <p/>
 * 
 * @author <a href="mailto:978954852@qq.com">xfjin</a>
 * @version  Date: 2016年3月22日 上午3:16:57
 * @serial 1.0
 * @since 2016年3月22日 上午3:16:57
 */



 @Component
 public class Memory {
 
     @Autowired
     private Cache ehcache;
 
     /**
      * 关闭缓存管理器
      */
     @PreDestroy
     protected void shutdown() {
         if (ehcache != null) {
             ehcache.getCacheManager().shutdown();
         }
     }
 
     /**
      * 保存当前登录用户信息
      * 
      * @param loginUser
      */
     /**
     public void saveLoginUser(LoginUser loginUser) {
         // 生成seed和token值
         String seed = MD5Util.getMD5Code(loginUser.getUsername());
         String token = TokenProcessor.getInstance().generateToken(seed, true);
         // 保存token到登录用户中
         loginUser.setToken(token);
         // 清空之前的登录信息
         clearLoginInfoBySeed(seed);
         // 保存新的token和登录信息
         String timeout = getSystemValue(SystemParam.TOKEN_TIMEOUT);
         int ttiExpiry = NumberUtils.toInt(timeout) * 60; // 转换成秒
         ehcache.put(new Element(seed, token, false, ttiExpiry, 0));
         ehcache.put(new Element(token, loginUser, false, ttiExpiry, 0));
     }*/
     /**
      * 获取当前线程中的用户信息
      * 
      * @return
      *//*
     public LoginUser currentLoginUser() {
         Element element = ehcache.get(ThreadTokenHolder.getToken());
         return element == null ? null : (LoginUser) element.getValue();
     }*/
 
     /**
      * 根据token检查用户是否登录
      * 
      * @param token
      * @return
      */
     /*
     public boolean checkLoginInfo(String token) {
         Element element = ehcache.get(token);
         
         return element != null && (LoginUser) element.getValue() != null;
     }*/ 

     /**
      * 清空登录信息
      */
     public void clearLoginInfo() {/*
        LoginUser loginUser = currentLoginUser();*/
       /* if (loginUser != null) {
             // 根据登录的用户名生成seed，然后清除登录信息
             String seed = MD5Util.getMD5Code(loginUser.getUsername());
             clearLoginInfoBySeed(seed);
         }*/
     }
 
    /**
     * 根据seed清空登录信息
      * 
     * @param seed
      */
     public void clearLoginInfoBySeed(String seed) {
         // 根据seed找到对应的token
         Element element = ehcache.get(seed);
         if (element != null) {
             // 根据token清空之前的登录信息
             ehcache.remove(seed);
             ehcache.remove(element.getValue());
         }
     }
}



 